package com.hbu.ware.hbumallware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.Query;
import com.hbu.ware.hbumallware.dao.PurchaseDetailDao;
import com.hbu.ware.hbumallware.entity.PurchaseDetailEntity;
import com.hbu.ware.hbumallware.service.PurchaseDetailService;
import com.hbu.ware.hbumallware.vo.PurchaseDetailListVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getList(PurchaseDetailListVO purchaseDetailList) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        Long wareId = purchaseDetailList.getWareId();
        if (null != wareId && wareId > 0) {
            queryWrapper.eq("ware_id", wareId);
        }
        String key = purchaseDetailList.getKey();
        if (StringUtils.hasText(key)) {
            queryWrapper.and(row -> row.eq("purchase_id", key).or().eq("sku_id", key));
        }
        Integer status = purchaseDetailList.getStatus();
        if (null != status) {
            queryWrapper.eq("status", status);
        }
        return new PageUtils(this.page(new Page<>(purchaseDetailList.getPageOrDefault(), purchaseDetailList.getLimitOrDefault()), queryWrapper));

    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        return baseMapper.selectList(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
    }

    @Override
    public void updateStatusByPurchaseId(List<Long> ids) {
        baseMapper.updateStatusByPurchaseId(ids);
    }

    @Override
    public void updateStatusById(Long itemId, Integer status) {
        baseMapper.updateStatusById(itemId, status);
    }

}